package com.lhn.spring5.beanpostprocessor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * 〈一句话功能简述>
 * 〈后置处理器〉
 *
 * @Author 17505
 * @Create 2021/12/29
 * @Since 1.0.0
 */
public class PasswordDecodePostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof PasswordDecodable) {
            String encodedPassword = ((PasswordDecodable)bean).getEncodedPassword();
            String decodedPassword = decodePassword(encodedPassword);
            ((PasswordDecodable)bean).setDecodedPassword(decodedPassword);
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    private String decodePassword(String encodedPassword) {
        // 实现解码逻辑 3
        return encodedPassword + "12312312312";
    }
}